﻿namespace Hidistro.SqlDal.Commodities
{
    using Hidistro.Core.Entities;
    using Hidistro.Entities;
    using Hidistro.Entities.Commodities;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Text;

    public class HomeProductDao
    {
        private Database database = DatabaseFactory.CreateDatabase();

        public bool AddHomeProdcut(int productId, ClientType client)
        {
            bool flag = true;
            string commandText = string.Format("SELECT COUNT(1) FROM Vshop_HomeProducts WHERE ProductId={0} AND Client = {1}", productId, (int) client);
            int num = (int) this.database.ExecuteScalar(CommandType.Text, commandText);
            if (num == 0)
            {
                DbCommand sqlStringCommand = this.database.GetSqlStringCommand("INSERT INTO Vshop_HomeProducts(ProductId, Client,DisplaySequence) VALUES (@ProductId, @Client,0)");
                this.database.AddInParameter(sqlStringCommand, "ProductId", DbType.Int32, productId);
                this.database.AddInParameter(sqlStringCommand, "Client", DbType.Int32, client);
                try
                {
                    return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
                }
                catch
                {
                    flag = false;
                }
            }
            return flag;
        }

        public DataTable GetHomeProducts(ClientType client)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select p.ProductId, ProductCode, ProductName,ShortDescription,ThumbnailUrl40,ThumbnailUrl160,ThumbnailUrl100,ThumbnailUrl180,ImageUrl9,MarketPrice, SalePrice,ShowSaleCounts,SaleCounts, Stock,t.DisplaySequence");
            builder.Append(" from vw_Hishop_BrowseProductList p inner join  Vshop_HomeProducts t on p.productid=t.ProductId ");
            builder.AppendFormat(" and SaleStatus = {0} WHERE Client = {1}", 1, (int) client);
            builder.Append(" order by t.DisplaySequence asc");
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(builder.ToString());
            return this.database.ExecuteDataSet(sqlStringCommand).Tables[0];
        }

        public DbQueryResult GetProducts(ProductQuery query)
        {
            query.IsIncludeHomeProduct = false;
            return new ProductDao().GetProducts(query);
        }

        public bool RemoveAllHomeProduct(ClientType client)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(string.Format("DELETE FROM Vshop_HomeProducts WHERE Client = {0}", (int) client));
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public bool RemoveHomeProduct(int productId, ClientType client)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("DELETE FROM Vshop_HomeProducts WHERE ProductId = @ProductId AND Client = @Client");
            this.database.AddInParameter(sqlStringCommand, "ProductId", DbType.Int32, productId);
            this.database.AddInParameter(sqlStringCommand, "Client", DbType.Int32, client);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public bool UpdateHomeProductSequence(ClientType client, int ProductId, int displaysequence)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("update Vshop_HomeProducts  set DisplaySequence=@DisplaySequence where ProductId=@ProductId AND Client = @Client");
            this.database.AddInParameter(sqlStringCommand, "@DisplaySequence", DbType.Int32, displaysequence);
            this.database.AddInParameter(sqlStringCommand, "@ProductId", DbType.Int32, ProductId);
            this.database.AddInParameter(sqlStringCommand, "Client", DbType.Int32, client);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }
    }
}

